package com.test.wade.testcanvas.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.test.wade.testcanvas.entity.SectorData;

import java.util.ArrayList;

public class SectorView extends View{
    //颜色表（注意：此处定义颜色使用的是ARGB，带Alpha通道的）
    private int [] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080, 0xFFE6B800, 0xFF7CFC00};
    //饼状图初始绘制角度
    private float mStartAngle = 0;
    //数据
    private ArrayList<SectorData> mData;
    //宽高
    private int mWidth,mHeight;
    //画笔
    private Paint mPaint = new Paint();

    public SectorView(Context context) {
        this(context,null,0);
    }

    public SectorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SectorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint.setStyle(Paint.Style.FILL);
        //抗锯齿
        mPaint.setAntiAlias(true);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式

        int heightsize = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式
        int height = 0;
        switch (heightmode){
            case MeasureSpec.AT_MOST:
                mWidth = 600;
                mHeight = 600;
                break;
            case MeasureSpec.EXACTLY:
                mWidth = widthsize;
                mHeight = heightsize;
            case MeasureSpec.UNSPECIFIED:
                mWidth = 600;
                mHeight = 600;
                break;
        }
        setMeasuredDimension(mWidth, mHeight);

    }

    /**
     * 布局大小发生变化
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == mData){
            return;
        }
        float currentStartAngle = mStartAngle;
        canvas.translate(mWidth / 2, mHeight / 2);
        float r = (float) (Math.min(mWidth, mHeight) / 2 * 0.8);
        RectF rect = new RectF(-r, -r, r, r);

        for (int i = 0; i < mData.size(); i++) {
            SectorData data = mData.get(i);
            mPaint.setColor(data.getColor());
            canvas.drawArc(rect, currentStartAngle, data.getAngle(), true, mPaint);
            currentStartAngle += data.getAngle();
        }
    }

    //设置起始角度
    public void setStartAngle(int mStartAngle){
        this.mStartAngle = mStartAngle;
        invalidate();//刷新
    }
    public void setData(ArrayList<SectorData> mData){
        this.mData = mData;
        initData(mData);
        invalidate();//刷新
    }
    //设置数据
    public void initData(ArrayList<SectorData> mData){
        if (null == mData || mData.size() == 0){
            return;
        }
        float sumValue = 0;
        for (int i = 0; i < mData.size(); i++) {
            SectorData data = mData.get(i);

            sumValue += data.getValue();

            int j = i % mColors.length;
            data.setColor(mColors[j]);
        }

        float sumAngle = 0;
        for (int i = 0; i < mData.size(); i++) {
            SectorData data = mData.get(i);

            float percentage = data.getValue() / sumValue;
            float angle = percentage * 360;

            data.setPercentage(percentage);
            data.setAngle(angle);
            sumAngle += angle;

            Log.i("angle"," " + data.getAngle());
        }
    }
}
